﻿#region

using System;
using System.IO;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using log4net;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class UploadController : Controller
    {
        private readonly UserService _userService;
        private readonly ExpertProfileService _profileService;

        public UploadController(UserService userService, ExpertProfileService profileService)
        {
            _userService = userService;
            _profileService = profileService;
        }

        [HttpPost]
        public ActionResult UploadLogo(HttpPostedFileBase file,int companyId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var username = _userService.GetUserByUsername(User.Identity.Name).Username;
                var directory = Server.MapPath("~/Content/Uploads/" + Path.Combine(username, "CompanyProfiles", companyId.ToString(), "Photo"));
                var dir = new DirectoryInfo(directory);
                if (!dir.Exists)
                    dir.Create();

                var filePath = Path.Combine(directory, file.FileName);
                System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
                var image = new WebImage(filePath);
                image.Resize(250, 250, true, false);
                image.Save(Path.Combine(directory, "250X250_" + file.FileName));
                image.Resize(150, 150, true, false);
                image.Save(Path.Combine(directory, "150X150_" + file.FileName));
                image.Resize(80, 80, true, false);
                image.Save(Path.Combine(directory, "80X80_" + file.FileName));
                image.Resize(50, 50, true, false);
                image.Save(Path.Combine(directory, "50X50_" + file.FileName));
                var path = "../../Content/Uploads/" + username + "/" + "CompanyProfiles" + "/" + companyId.ToString() +  "/" + "Photo" + "/" + "250X250_" + file.FileName;
                return Json(path);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Content("ERROR");
            }
        }

        [HttpPost]
        public ActionResult UploadPhoto(HttpPostedFileBase file, int profileId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var username = _userService.GetUserByUsername(User.Identity.Name).Username;
                var directory = Server.MapPath("~/Content/Uploads/" + Path.Combine(username, "ExpertProfiles", profileId.ToString(), "Photo"));
                var dir = new DirectoryInfo(directory);
                if (!dir.Exists)
                    dir.Create();

                var filePath = Path.Combine(directory, file.FileName);
                System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
                var image = new WebImage(filePath);
                image.Resize(250, 250, true, false);
                image.Save(Path.Combine(directory, "250X250_" + file.FileName));
                image.Resize(150, 150, true, false);
                image.Save(Path.Combine(directory, "150X150_" + file.FileName));
                image.Resize(80, 80, true, false);
                image.Save(Path.Combine(directory, "80X80_" + file.FileName));
                image.Resize(50, 50, true, false);
                image.Save(Path.Combine(directory, "50X50_" + file.FileName));
                var path = "../../Content/Uploads/" + username + "/" + "ExpertProfiles" + "/" + profileId.ToString() + "/" + "Photo" + "/" + "250X250_" + file.FileName;
                return Json(path);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Content("ERROR");
            }
        }

        [HttpPost]
        public ActionResult UploadProjectPhoto(HttpPostedFileBase file, int projectId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var username = _userService.GetUserByUsername(User.Identity.Name).Username;
                var directory = Server.MapPath("~/Content/Uploads/" + Path.Combine(username, "ProjectProfiles", projectId.ToString(), "Photo"));
                var dir = new DirectoryInfo(directory);
                if (!dir.Exists)
                    dir.Create();

                var filePath = Path.Combine(directory, file.FileName);
                System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
                var image = new WebImage(filePath);
                image.Resize(250, 250, true, false);
                image.Save(Path.Combine(directory, "250X250_" + file.FileName));
                image.Resize(150, 150, true, false);
                image.Save(Path.Combine(directory, "150X150_" + file.FileName));
                image.Resize(80, 80, true, false);
                image.Save(Path.Combine(directory, "80X80_" + file.FileName));
                image.Resize(50, 50, true, false);
                image.Save(Path.Combine(directory, "50X50_" + file.FileName));
                var path = "../../Content/Uploads/" + username + "/" + "ProjectProfiles" + "/" + projectId.ToString() + "/" + "Photo" + "/" + "250X250_" + file.FileName;
                return Json(path);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Content("ERROR");
            }
        }

        [HttpPost]
        public ActionResult UploadCv(HttpPostedFileBase file, int profileId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var username = _userService.GetUserByUsername(User.Identity.Name).Username;
                var directoryName = Server.MapPath("~/Content/Uploads/" + Path.Combine(username, "ExpertProfiles", profileId.ToString(), "Cv"));
                var di = new DirectoryInfo(directoryName);
                if (!di.Exists)
                    di.Create();

                // delete first all existing files
                foreach (var fi in di.GetFiles())
                {
                    fi.Delete();
                }
                var filePath = Path.Combine(directoryName, "Cv_" + file.FileName);
                System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
                var path = "../../Content/Uploads/" + username + "/" + "ExpertProfiles" + "/" + profileId.ToString() + "/" + "Cv" + "/" + "Cv_" + file.FileName;
                return Json(path);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Content("ERROR");
            }
        }

        private byte[] ReadData(Stream stream)
        {
            byte[] buffer = new byte[16*1024];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                return ms.ToArray();
            }
        }
    }
}